<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VideoController extends Controller
{
    public function list()
    {
        $videos = Video::orderby('id') // 获取 Video 模型的数据，并按照 id 字段排序
        ->paginate(10); // 对数据进行分页，每页显示 5 条记录
        return view('admin.video_list') // 返回 admin.video_list 视图
        ->with('videos', $videos); // 将分页后的视频数据传递给视图
    }
    public function create() // 定义创建方法
    {
        $oExpert = Expert::all(); // 获取所有专家数据
        return view('admin.video_add') // 返回 admin.video_add 视图
        ->with('oExpert', $oExpert); // 将专家数据传递给视图
    }
    public function review() // 定义审查方法
    {
        $videos = Video::where('type','=','0')->orderby('id')->paginate(6); // 按 id 排序并分页获取视频数据
        return view('front.review') // 返回前台审查视图
        ->with('videos', $videos); // 将视频数据传递给视图
    }
    public function show($id) // 定义显示特定视频的方法
    {
        $oExpert = Expert::find($id); // 根据 id 查找专家
        $expert = Expert::where('id', '!=', $oExpert->id)->take(1)->get(); // 获取除当前专家外的一个专家
        $video = Video::find($id); // 根据 id 查找视频
        $oVideos = Video::where('id', '=', $video->id)->take(1)->get(); // 获取当前视频
        return view('front.review_info') // 返回前台审查信息视图
        ->with('oVideos', $oVideos) // 将相关视频数据传递给视图
        ->with('video', $video) // 将视频传递给视图
        ->with('expert', $expert); // 将专家传递给视图
    }
    public function store(Request $request)  // 定义一个存储方法，接收请求对象
    {
        $data = $request->all();  // 获取请求中的所有数据
        $rules=[
            'doc_id'=>'required',
            'title'=>'required',
            'video'=>'required',
            'start_at'=>'required',
            'end_at'=>'required',
            'info'=>'required',
        ];
        $message=[
            'doc_id.required'=>'关联专家不能为空',
            'title.required'=>'标题不能为空',
            'video.required'=>'视频路径不能为空',
            'start_at.required'=>'开始时间不能为空',
            'end_at.required'=>'结束时间不能为空',
            'info.required'=>'视频简介不能为空',
        ];
        $validate=Validator::make($data,$rules,$message);
        if ($validate->fails()){
            return view('admin.fail')->with('message',$message);
        }
        if (isset($data['img_thumb'])){
            $data['image']=$data['img_thumb'];
        }else{
            $data['image']='/assets/test_images/r1.jpg';
        }
        $expert=Expert::find($data['doc_id'][0]);
        $data['expert']=$expert->name;
        $video=Video::create($data);
        foreach ($data['doc_id'] as $v){
            $video->Middle()->attach((int)$v);
        }
        $value='video';
        return view('admin.success')->with('value',$value);
    }
    public function edit($id)
    {
        $avideo=Video::find($id); // 根据 ID 查找视频
        $oExpert = Expert::orderby('id')->get(); // 根据 ID 查找专家信息并获取
        return view('admin.video_edit')->with('oExpert', $oExpert)->with('avideo', $avideo); // 返回视图并传递专家信息和视频信息
    }
    public function update(Request $request, $id)
    {
        $data = $request->all(); // 获取请求中的所有数据
        $rules=[
            'doc_id'=>'required',
            'title'=>'required',
            'video'=>'required',
            'start_at'=>'required',
            'end_at'=>'required',
            'info'=>'required',
        ];
        $message=[
            'doc_id.required'=>'关联专家不能为空',
            'title.required'=>'标题不能为空',
            'video.required'=>'视频路径不能为空',
            'start_at.required'=>'开始时间不能为空',
            'end_at.required'=>'结束时间不能为空',
            'info.required'=>'视频简介不能为空',
        ];
        $validate=Validator::make($data,$rules,$message);
        if ($validate->fails()){
            return view('admin.fail')->with('message',$message);
        }
        $video = Video::find($id); // 根据 ID 查找视频
        foreach ($video->middle as $v){
            $video->Middle()->detach($v);
        }
        if(isset($data['img_thumb'])){
            $data['image']=$data['img_thumb'];
        }else{
            $data['image']=$video['image'];
        }
        $video->update($data); // 更新视频数据
        foreach ($data['doc_id'] as $v){
            $video->Middle()->attach((int)$v);
        }
        $value='video';
        return view('admin.success')->with('value',$value);
    }
    public function delete($id)
    {
        $videos = Video::find($id); // 根据 ID 查找视频
        $videos->delete(); // 删除找到的视频
        $value='video';
        return view('admin.success')->with('value',$value);
    }
    public function show_review($id){
        $video=Video::find($id);
        $oVideos=Video::where('type','=','0')->where('id','!=',$id)->orderby('id','desc')->get();
        return view('front.review_info')->with('video',$video)->with('oVideos',$oVideos);
    }
    public function index(){
        $now_time= time();
        $now_date= date('Y-m-d H:i:s',$now_time);
        $oVideos=Video::where('type','=',0)->orderby('start_at','desc')->get();
        $qVideo=Video::where('start_at', '>', $now_date)->where('type','=',1)->orderby('start_at')->get();
        $zVideo=Video::where('start_at', '<=', $now_date)->where('end_at', '>', $now_date)->where('type','=',1)->orderby('start_at')->get();
        return view('front.index')->with('qVideo',$qVideo)->with('zVideo',$zVideo)->with('oVideos',$oVideos);
    }
    public function lecture(){
        $now_time= time();
        $now_date= date('Y-m-d H:i:s',$now_time);
        $oVideos=Video::where('type','=',0)->orderby('start_at','desc')->take(3)->get();
        $qVideo=Video::where('start_at', '>', $now_date)->where('type','=',1)->orderby('start_at')->get();
        $zVideo=Video::where('start_at', '<=', $now_date)->where('end_at', '>', $now_date)->where('type','=',1)->orderby('start_at')->get();
        return view('front.lecture')->with('qVideo',$qVideo)->with('zVideo',$zVideo)->with('oVideos',$oVideos);
    }
    public function upload()
    {
        $allow_type = ["image/jpeg", "image/gif", "image/png"];
        if (!in_array($_FILES['upload_file']['type'], $allow_type)) {
            $msg = '图片类型不符合要求';
            return json_encode(['success' => false, 'msg' => $msg]);
        } elseif ($_FILES['upload_file']['size'] > 2000000) {
            $msg = '图片大小不符合要求';
            return json_encode(['success' => false, 'msg' => $msg]);
        } elseif ($_FILES['upload_file']['error'] != 0) {
            $msg = '上传出错';
            return json_encode(['success' => false, 'msg' => $msg]);
        } else {
            $src_image = $_FILES['upload_file']['tmp_name'];
            $size = getimagesize($_FILES['upload_file']['tmp_name']);
            $src_width = $size[0];
            $src_height = $size[1];
            $tar_width = 400;
            $tar_height = $tar_width / $src_width * $src_height;
            $tar_thumb = imagecreatetruecolor($tar_width, $tar_height);
            if ($_FILES['upload_file']['type'] == 'image/jpeg') {
                $src_thumb = imagecreatefromjpeg($src_image);
            } elseif ($_FILES['upload_file']['type'] == "image/gif") {
                $src_thumb = imagecreatefromgif($src_image);
            } else {
                $src_thumb = imagecreatefrompng($src_image);
            }
            imagecopyresized($tar_thumb, $src_thumb, 0, 0, 0, 0, $tar_width, $tar_height, $src_width, $src_height);
            $path = './uploads/' . $_FILES['upload_file']['name'];
            imagejpeg($tar_thumb, $path);
            return json_encode(['success' => true, 'img_thumb' => '/uploads/' . $_FILES['upload_file']['name']]);
        }
    }
}
